package com.ds.infra.ibatis.demo.config;

import com.ds.infra.ibatis.session.DefaultSqlSessionFactory;
import com.ds.infra.ibatis.session.SqlSessionFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;

import javax.sql.DataSource;

/**
 * 作用: 配置类，配置SqlSessionFactory
 *
 * @author WeiShaoying
 */
@Configuration
// @Enable
public class IbatisConfig {

    @Bean
    public com.ds.infra.ibatis.session.Configuration ibatisConfiguration(DataSource dataSource) {
        // 自动注入数据源，通过数据源创建Configuration对象
        com.ds.infra.ibatis.session.Configuration configuration = new com.ds.infra.ibatis.session.Configuration();
        configuration.setDataSource(dataSource);
        return configuration;
    }

    @Bean
    public SqlSessionFactory sqlSessionFactory(com.ds.infra.ibatis.session.Configuration configuration) {
        return new DefaultSqlSessionFactory(configuration);
    }

    @Bean
    public PlatformTransactionManager transactionManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }
}
